package com.fyzk.activity.main;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.fyzk.activity.app.App;
import com.fyzk.base.BaseActivity;
import com.fyzk.net.Commands;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.ScreenUtils;
import com.fyzk.utils.Utils;
import com.fyzk.view.TopTitleView;
import com.fyzk.view.pagercycle.ADInfo;
import com.tyjc.lottery.R;
import com.wf.domain.UrlDef;

import org.json.JSONObject;
import org.xutils.common.Callback.CommonCallback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动页面
 */
public class PromotionActivity extends BaseActivity {

    private TopTitleView topView;
    ListView lv_promotion;
    private List<ADInfo> infos = new ArrayList<>();
    PromotionAdapter promotionAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_layout);
        topView = (TopTitleView) findViewById(R.id.news_toptitle);
        lv_promotion = (ListView) findViewById(R.id.lv_promotion);
        topView.setRightVisible(View.GONE);
        topView.setOnTitleClickListener(this);
        topView.setCenterText(R.string.promotion);
        promotionAdapter = new PromotionAdapter();
        lv_promotion.setAdapter(promotionAdapter);
        lv_promotion.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(PromotionActivity.this, PromotionDetailActivity.class);
                intent.putExtra("news", infos.get(i));
                startActivity(intent);
            }
        });
        requestAD();
    }


    private void requestAD() {
        RequestParams params = new RequestParams(UrlDef.NEWS_URL + Commands.NEWSLIST);
        params.addBodyParameter("category", "android_client_news");
        params.addBodyParameter("page", "1");
        params.addBodyParameter("perpage", "10");
        params.addBodyParameter("src", App.getChannel());
        sendRequest(HttpMethod.GET, params);
    }

    public void sendRequest(HttpMethod method, RequestParams params) {

        if (!Utils.isNetworkAvailable(this)) {
            return;
        }
        x.http().request(method, params, new
                CommonCallback<String>() {
                    public void onSuccess(String result) {
                        try {
                            JSONObject object = new JSONObject(result);
                            List<ADInfo> resultBeans = JSON.parseArray(
                                    object.getString("data"), ADInfo.class);
                            infos.clear();
                            infos.addAll(resultBeans);
                            promotionAdapter.notifyDataSetChanged();
                        } catch (Exception e) {

                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
    }


    class PromotionAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return infos.size();
        }

        @Override
        public ADInfo getItem(int i) {
            return infos.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertview, ViewGroup parentView) {
            ViewHolder holder;
            if (convertview == null) {
                holder = new ViewHolder();
                convertview = View.inflate(PromotionActivity.this, R.layout.layout_item_promotion, null);
                holder.imageView  = convertview.findViewById(R.id.img_promotion);
                holder.txtView  = convertview.findViewById(R.id.title_promotion);
                convertview.setTag(holder);
            }else{
                holder = (ViewHolder) convertview.getTag();
            }
            ADInfo info = getItem(position);

            int screenWidth = ScreenUtils.getInstance().getWidth()- PublicMethod.dip2pix(16);
            int height = screenWidth * 360 / 640;
            holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(screenWidth, height));
            RequestBuilder<Drawable> thumbnailRequest = Glide
                    .with(getContext())
                    .load(R.drawable.img_top_default);
            Glide.with(getContext()).load(info.icon_url)
                    .thumbnail(thumbnailRequest)
                    .into(holder.imageView );
            holder.txtView.setText(info.title);
            return convertview;
        }
    }

    class ViewHolder{
        ImageView imageView;
        TextView txtView;
    }


}
